package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.DdepDao;
import cn.gson.fengboot.model.dao.DdepandmenusDao;
import cn.gson.fengboot.model.dao.DmenusDao;
import cn.gson.fengboot.model.dao.DrolesmenuDao;
import cn.gson.fengboot.model.mapper.DmenusMapper;
import cn.gson.fengboot.model.pojos.Dbusiness;
import cn.gson.fengboot.model.pojos.QdepFrom;
import cn.gson.fengboot.model.pojos.QempFrom;
import cn.gson.fengboot.model.pojos.QmenusEntity;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class)
public class DmenusService {

    @Autowired
    DmenusMapper dmenusMapper;

    @Autowired
    DdepDao ddepDao;

    @Autowired
    DmenusDao dmenusDao;

    @Autowired
    DdepandmenusDao ddepandmenusDao;

    @Autowired
    DrolesmenuDao drolesmenuDao;

    public List<QmenusEntity> myallmenus(){

        return dmenusMapper.myallmenus();
    }

    public List<QmenusEntity> mydepallmenus(Integer depId){
        return dmenusMapper.mydepallmenus(depId);
    }

    public void mydepandmenus(String mydepandmenus){
        JSONObject obj = (JSONObject)JSONObject.parse(mydepandmenus);
        Integer depId=obj.getObject("depId",Integer.class);
        List menuslist = obj.getObject("menuslist", List.class);

        System.out.println("我在找你");
        System.out.println(menuslist.toString());

        QdepFrom byDepId = ddepDao.findByDepId(depId);
        Collection<QmenusEntity> menuslist1 = byDepId.getMenuslist();
        if(menuslist1.isEmpty()){
            menuslist1 = (Collection)new ArrayList<>();
        }

        menuslist1.clear();
        ddepandmenusDao.deleteAllByDepId(depId);//删除已有的权限
        menuslist1.addAll((Collection)dmenusDao.findAllById(menuslist));
        byDepId.setMenuslist(menuslist1);
        ddepDao.save(byDepId);


    }

}
